package com.swu.pad.comment.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.swu.pad.activity.entity.Activity;
import com.swu.pad.activity.service.ActivityService;
import com.swu.pad.assignment.dao.AssignmentDao;
import com.swu.pad.assignment.dao.AssignmentPadDao;
import com.swu.pad.assignment.entity.Assignment;
import com.swu.pad.assignment.entity.AssignmentPad;
import com.swu.pad.comment.dao.CommentQuestionDao;
import com.swu.pad.comment.dao.CommentTemplateDao;
import com.swu.pad.comment.entity.CommentQuestion;
import com.swu.pad.comment.entity.CommentTemplate;
import com.swu.pad.comment.service.CommentTemplateService;
import com.swu.pad.course.entity.CourseGroupUser;
import com.swu.pad.course.entity.Group;
import com.swu.pad.course.service.CourseService;

@Service
@Transactional
public class CommentTemplateServiceImpl implements CommentTemplateService {

	@Autowired
	private CommentTemplateDao commentTemplateDao;
	@Autowired
	private CommentQuestionDao commentQuestionDao;
	@Autowired
	private AssignmentPadDao assignmentPadDao;
	@Autowired
	private ActivityService activityService;
	@Autowired
	private CourseService courseService;
	@Autowired
	private AssignmentDao assignmentDao;

	public List<CommentTemplate> getTemplates() {
		return commentTemplateDao.findAll();
	}

	public CommentTemplate saveOrUpateTemplate(CommentTemplate commentTemplate) {
		return commentTemplateDao.saveAndFlush(commentTemplate);
	}

	public void deleteTemplate(String templateId) {
		commentTemplateDao.delete(templateId);
	}

	public List<CommentQuestion> getTemplateQuestions(String templateId) {
		return commentQuestionDao.findByTemplateId(templateId);
	}

	public CommentQuestion saveOrUpateQuestion(CommentQuestion commentQuestion) {
		return commentQuestionDao.saveAndFlush(commentQuestion);
	}

	public void deleteQuestion(String questionId) {
		commentQuestionDao.delete(questionId);

	}

	public Map<String, Object> getCommentQuestions(String authorId, String assignmentId) {
		// {assignmentId:'',assignmentPadId:'',commentTemplateId:'',currentActivityType:'',commentQuestion:[]}
		List<CourseGroupUser> courseGroupUserList = courseService.getUserAssignmentGroup(authorId, assignmentId, Group.TYPE_GROUP);
		

		
		AssignmentPad assignmentPad = assignmentPadDao.findAssignmentPadByAssignmentIdAndGroupId(assignmentId, courseGroupUserList.get(0).getGroupId());
    
	
		//注释学长的代码 Activity activity = activityService.getAssignmentCurrentActivity(assignmentId);
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		map.put("assignmentId", assignmentId);
		map.put("assignmentPadId", assignmentPad.getId());
	 
		//注释学长的代码 map.put("commentTemplateId", activity.getCommentTemplateId());
		//注释学长的代码 map.put("currentActivityType", activity.getActivityType());
		List<Map<String, Object>> tempList = new ArrayList<Map<String, Object>>();
		
		for (int i = 0; i < courseGroupUserList.size(); i++) {
			Map<String, Object> tempMap = new HashMap<String, Object>();
			if (courseGroupUserList.get(i).getUserId().equals(authorId))
				continue;//如果if条件不成立，跳出本次循环。则不执行下面的语句。
			
			
			tempMap.put("userId", courseGroupUserList.get(i).getUserId());
			tempMap.put("userName", courseGroupUserList.get(i).getUserName());
			
			//z s
			Assignment assignment = assignmentDao.findOne(assignmentId);
	       	List<CommentQuestion>  commentQuestionsList=commentQuestionDao.findByTemplateId(assignment.getMutualTemplateId());
	       	
	       	//z e
	       
	       	//注释学长的代码 List<CommentQuestion> commentQuestionList = commentQuestionDao.findByTemplateId(activity.getCommentTemplateId());
			//注释学长的代码 tempMap.put("commentQuestion", commentQuestionList);
			tempMap.put("commentQuestion",commentQuestionsList);
			
			tempList.add(tempMap);
			
		}
		map.put("comment", tempList);
		
		
	/*	for(int i=0;i<courseGroupUserList.size();i++){
			if(courseGroupUserList.get(i).getUserId().equals(authorId)){
				courseGroupUserList.remove(courseGroupUserList.get(i));
			}
		}
		
		for(int i=0;i<courseGroupUserList.size();i++){
			System.out.println("小组成员信息 "+courseGroupUserList.get(i).getUserName());
		
		
		
		map.put("myGroupUserExceptMe",courseGroupUserList);*/
		
		
		return map;
	}

}
